﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using Abp.Runtime.Caching;
using Abp.UI;
using AutoMapper.QueryableExtensions;
using FwProject.Car;
using FwProject.Distribution.Dto;
using FwProject.Users;
using Microsoft.AspNetCore.Hosting;
using X.PagedList;

namespace FwProject.Distribution
{
    [AutoMap(typeof(DriverLine))]
    public class DriverLineAppService: FwProjectAppServiceBase, IDriverLineAppService
    {
        public readonly IRepository<DriverLine, Guid> _repositoryDriverLine;
        public readonly IUserAppService _userAppService;
        public readonly ICarInfoAppService _carAppService;
        public readonly ICacheManager _catchmanager;
        public readonly IHostingEnvironment _env;
        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }

        public DriverLineAppService(IRepository<DriverLine, Guid> repositoryDriverLine, ICacheManager catchmanager, IHostingEnvironment env, ICarInfoAppService carAppService, IUserAppService userAppService)
        {
            _guid = SequentialGuidGenerator.Instance;
            _repositoryDriverLine = repositoryDriverLine;
            _catchmanager = catchmanager;
            _env = env;
            _carAppService = carAppService;
            _userAppService = userAppService;
        }
        /// <summary>
        /// 分配司机绑定线路
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Guid> Create(DriverLineInput input)
        {
            var driver = _repositoryDriverLine.GetAll().Where(o => o.DriverId == input.DriverId).FirstOrDefault();
            if (driver != null)
                throw new UserFriendlyException("当前司机已绑定该路线！");

            var model = _map.Map<DriverLine>(input);
            model.Id = _guid.Create();
            return await _repositoryDriverLine.InsertAndGetIdAsync(model);
        }
        /// <summary>
        /// 修改一个司机配送线路
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task Update(Guid id, DriverLineInput input)
        {
            var entity =  _repositoryDriverLine.GetAll().Where(o => o.DriverId == input.DriverId).FirstOrDefault();
            if (entity != null)
                throw new UserFriendlyException(200,"该司机已绑定其他线路！");
            var model = await _repositoryDriverLine.GetAsync(id);
            model = _map.Map(input, model);
            await _repositoryDriverLine.UpdateAsync(model);
        }

        public async Task Delete(Guid id)
        {
            var model = await _repositoryDriverLine.GetAsync(id);
            await _repositoryDriverLine.DeleteAsync(model);
        }

        public async Task<DriverLineDto> GetByIdAsync(Guid id)
        {
            var model = await _repositoryDriverLine.GetAsync(id);
            return _map.Map<DriverLineDto>(model);
        }
        /// <summary>
        /// 根据线路查询所有的司机信息
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <param name="pageSize"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<IPagedList<DriverLineDto>> GetPagedListAsync(int pageIdx,int pageSize,string name)
        {
            var usermodel = await _userAppService.GetByIdUserAsync(AbpSession.UserId.Value);
            var listmodel = _repositoryDriverLine.GetAllIncluding(o=>o.EmployeeDriver,o=>o.DistributionLineModel).AsQueryable();

            if (!string.IsNullOrEmpty(name))
                listmodel = listmodel.Where(o => o.DistributionLineModel.LineName.Contains(name));
            
            return await listmodel.Where(o=>o.DistributionLineModel.FactoryInfoid==usermodel.FactoryinfoId).ProjectTo<DriverLineDto>().ToPagedListAsync(pageIdx, pageSize);
        }
    }
}
